<template>
	<view class="c-container no-border order-container">
		<!-- 基本信息 -->
		<common-basic :isPreview="isPreview" :order="order" :orderParams="orderParams" :weddingParams="weddingParams"
			:banquetParams="banquetParams" :orderNature="orderNature" :orderType="orderType" :mode="mode"
			@resetValue="resetValue" @resetSettlement="resetSettlement" @getConfirmSequence="getConfirmSequence" />

		<!-- 酒店信息 -->
		<hotel :order="order" :orderParams="orderParams" :hallList="hallList" @chooseSearch="chooseSearch" />

		<!-- 非宝宝宴、会务的联系方式，婚期等 -->
		<common-contact :isPreview="isPreview" v-if="!['7', '8', '11', '12'].includes(orderParams.news_type)"
			:orderParams="orderParams" :order="order" :startDate="startDate" :endDate="endDate" />

		<!-- 宝宝宴类型订单的联系方式、婚期等 -->
		<baby-contact v-if="orderParams.news_type == '7' && [3, 4, 2].includes(orderParams.order_nature)"
			:orderParams="orderParams" :order="order" :startDate="startDate" :endDate="endDate" />

		<!-- 定金 -->
		<view class="c-row-box" v-if="orderParams.order_nature == 3">
			<view class="c-row-head">
				<view class="c-row-title">定金金额</view>
			</view>
			<view class="c-row-item">
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">定金金额</view>
						<view class="col-text"><input type="text" v-model="orderParams.prepay_money"
								:disabled="isPreview" placeholder="请输入" /></view>
					</view>
				</view>
			</view>
		</view>

		<!-- 意向金 -->
		<view class="c-row-box" v-if="orderParams.order_nature == 4">
			<view class="c-row-head">
				<view class="c-row-title">意向金额</view>
			</view>
			<view class="c-row-item">
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">意向金额</view>
						<view class="col-text"><input type="text" v-model="orderParams.prepay_money"
								:disabled="isPreview" placeholder="请输入" /></view>
					</view>
				</view>
			</view>
		</view>

		<!-- 销售信息 -->
		<sale :isPreview="isPreview" v-if="!include(orderParams.news_type, [7, 8, 11, 12])"
			:orderParams="orderParams" />

		<!-- 婚宴信息 -->
		<banquet :isPreview="isPreview"
			v-if="['0', '2'].includes(orderParams.news_type) && ![3, 2, 4].includes(orderParams.order_nature)"
			:banquetParams="banquetParams" :order="order" @getOrderTotals="getOrderTotals"
			@getOtherPrice="getOtherPrice" />

		<!-- 婚庆信息 -->
		<wedding :isPreview="isPreview"
			v-if="['1', '2'].includes(orderParams.news_type) && ![3, 2, 4].includes(orderParams.order_nature)"
			:order="order" :weddingParams="weddingParams" @getOrderTotals="getOrderTotals"
			@getOtherPrice="getOtherPrice" />

		<!-- 宝宝宴 -->
		<baby v-if="orderParams.news_type == '7' && ![3, 2, 4].includes(orderParams.order_nature)"
			:isPreview="isPreview" :babyParams="babyParams" :orderParams="orderParams" :order="order"
			:startDate="startDate" :endDate="endDate" @getOrderTotals="getOrderTotals" />

		<!-- 会务信息 -->
		<meeting :isPreview="isPreview"
			v-if="['8'].includes(orderParams.news_type) && ![3, 4].includes(orderParams.order_nature)" :parent="_self"
			:addType="addType" :meetingParams="meetingParams" :orderParams="orderParams"
			@getOrderTotals="getOrderTotals" @getOtherPrice="getOtherPrice"></meeting>

		<!-- 活动信息 11,12 activities -->
		<activities :isPreview="isPreview" :activitiesParams="activitiesParams" :orderParams="orderParams"
			v-if="orderParams.news_type == '11' || orderParams.news_type == '12'" :parent="_self"></activities>

		<!-- 其他项目列表 -->
		<other-item-list v-if="orderParams.order_nature != 5 && addons.length > 0" :addons="addons" :parent="_self">
		</other-item-list>

		<!-- 婚车信息 -->
		<car :isPreview="isPreview" v-if="orderParams.news_type == '4' || hasCarPresent" :carParams="carParams"
			@getOrderTotals="getOrderTotals" />


		<!-- 结算信息 -->
		<commission :isPreview="isPreview" v-if="orderParams.order_nature == 2" :settleParams="settleParams"
			:orderParams="orderParams" :startDate="startDate" :endDate="endDate" />

		<!-- 收款信息 -->
		<income :isPreview="isPreview"
			v-if="![0, 2].includes(orderParams.order_nature) && !['1'].includes(orderParams.cooperation_mode)  && !include(orderParams.news_type, [11,12])"
			:incomeParams="incomeParams" :orderParams="orderParams" :startDate="startDate" :endDate="endDate"
			:incomeNature="orderParams.order_nature" :incomeType="orderParams.news_type" @getTailMoney="getTailMoney"
			@resetIncome="resetIncome" />

		<!-- 定金收款 -->
		<earnest-money :isPreview="isPreview" v-if="
				incomeParams.income_item_price &&
					![0, 2].includes(orderParams.order_nature) &&
					incomeParams.income_type == 1 &&
					orderParams.order_nature != 3 &&
					orderParams.order_nature != 4 &&
					orderParams.order_nature != 5
			" :orderParams="orderParams" :incomeParams="incomeParams" :startDate="startDate" :endDate="endDate" />

		<!-- 中款收款 -->
		<middle-money :isPreview="isPreview" v-if="
				incomeParams.income_item_price &&
					![0, 2].includes(orderParams.order_nature) &&
					incomeParams.income_type == 1 &&
					orderParams.order_nature != 3 &&
					orderParams.order_nature != 4 &&
					orderParams.order_nature != 5 &&
					orderParams.news_type!=7
			" :orderParams="orderParams" :startDate="startDate" :endDate="endDate" @getTailMoney="getTailMoney"
			@getMiddleChange="getMiddleChange" />

		<!-- 尾款收款 -->
		<tail-money :isPreview="isPreview" v-if="
				orderParams.middle_money != '' &&
					![0, 2].includes(orderParams.order_nature) &&
					incomeParams.income_type == 1 &&
					orderParams.order_nature != 3 &&
					orderParams.order_nature != 4 &&
					orderParams.order_nature != 5
			" :orderParams="orderParams" :startDate="startDate" :endDate="endDate" @getMiddleMoney="getMiddleMoney"
			@getTailChange="getTailChange" />

		<!-- 合同信息 -->
		<contract-note :isPreview="isPreview" :orderParams="orderParams"
			v-if="!include(orderParams.news_type, [11,12])" />

		<!-- 底部栏 -->
		<view class="footer-box">
			<view class="footer-text">
				<text>合同总额：{{ orderParams.totals }}</text>
			</view>
			<view class="footer-action-btn"><button size="mini" type="primary" disabledColor="#ffffff"
					:disabled="disabled" @click="submit">创建订单</button></view>
		</view>


		<!-- 添加、查看赠送项目 -->
		<append-other-item ref="appendOtherItem" :parent="_self"></append-other-item>

		<!-- 审核流程 -->
		<view class="confirm-list" v-if="confirmStep.length > 0">
			<divider title="审核流程"></divider>
			<steps-look ref="step" :stepList="confirmStep"></steps-look>
		</view>
	</view>
</template>

<script>
	import request from '@/utils/request.js';
	import dictionary from '@/utils/dictionary.js';
	import {
		checkObjLen,
		include
	} from '@/utils/common.js';

	// 订单的基本信息,信息类型、订单性质、签约公司、合同号等
	import commonBasic from './common/basic.vue';
	// 酒店信息(筛选)、包含厅信息
	import hotel from './common/hotel.vue';
	// 非[宝宝宴|会务]订单的联系方式
	import commonContact from './common/contact.vue';
	// 宝宝宴订单的联系方式
	import babyContact from './babay/contact.vue';
	// 引流销售信息
	import sale from './common/sale.vue';
	// 婚宴信息（餐标、桌数、优惠金额等信息）
	import banquet from './banquet/detail.vue';
	// 婚庆信息（婚庆套餐等）
	import wedding from './wedding/detail.vue';
	// 婚车信息 （已选择的车型）
	import car from './car/list.vue';
	// 宝宝宴的详情（餐标桌数等）
	import baby from './babay/detail.vue';
	// 会务的详情（布置费用等）
	import meeting from './meeting/detail.vue';
	// 活动信息
	import activities from './activities/detail.vue';
	// 返佣单信息
	import commission from './common/commission.vue';
	// 收款信息
	import income from './common/income.vue';
	// 合同的定金、中款、尾款金额
	import earnestMoney from './common/contract.earnest.vue';
	import middleMoney from './common/contract.middle.vue';
	import tailMoney from './common/contract.tail.vue';
	// 合同的小票信息
	import contractNote from './common/contract.note.vue';
	// 其他赠送项目
	import otherItemList from './otherItem/list.vue';
	// 添加其他项目
	import appendOtherItem from './otherItem/append.vue';
	// 新版审核流程
	import stepsLook from '@/component/steps-look/steps-look.vue';
	import divider from '@/component/divider/divider';

	export default {
		components: {
			commonBasic,
			hotel,
			commonContact,
			babyContact,
			sale,
			banquet,
			wedding,
			car,
			baby,
			meeting,
			activities,
			commission,
			income,
			earnestMoney,
			middleMoney,
			tailMoney,
			contractNote,
			otherItemList,
			stepsLook,
			divider,
			appendOtherItem
		},
		data() {
			return {
				hallList: [],
				hasCarPresent: false,
				member_id: '', // 客资ID
				member_allocate_id: '',
				order_id: '', // 订单id
				// 其他费用列表
				addons: [],
				isPreview: true,
				order: {},
				// 公司列表
				companyList: [],

				// 增项项目
				addType: [],

				// 婚庆套餐
				weddingPackage: [],
				// 订单性质
				orderNature: [],
				// 订单类型
				orderType: [],
				// 合作模式
				mode: [],
				// 接口传参
				orderParams: {},
				banquetParams: {},
				babyParams: {},
				meetingParams: {},
				activitiesParams: {},
				carParams: [],
				settleParams: {},
				weddingParams: {},
				otherPirce: 0,
				incomeParams: {},
				confirmStep: [], // 审核流程
				ritualList: [], // 仪式列表
				tagTitleList: '',
				disabled: false
			};
		},
		onLoad(options) {
			// 预览
			if (options.member_id) {
				this.member_id = options.member_id;
				this.getTorge();
				this.getConfirmSequence();
			}

			// 从审核进
			if (options.order_id) {
				this.order_id = options.order_id;
				this.getOrderDetail(options);
			}
		},
		beforeMount() {
			this.checkObjLen = checkObjLen;
			this.dictionary = dictionary;
			this.include = include;
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			// 添加其他售卖信息
			showAppendOtherItemDialog(isEdit, item) {
				this.$refs["appendOtherItem"].showDialog(isEdit, item)
			},
			// 获取审核信息
			getConfirmSequence() {
				let that = this;
				request.request({
					url: '/api/order.audit/getAuditProcess',
					showLoading: true,
					token: true,
					data: Object.assign({
						order: that.orderParams
					}, {
						audit_type: that.orderParams.audit_type
					}),
					success(res) {
						if (res.code == 200) {
							that.confirmStep = res.data.process;
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							});
						}
					}
				});
			},

			// 获取订单信息
			getOrderDetail(e) {
				let that = this;
				let params = {
					id: e.order_id,
					user: e.user ? 'confirm_user' : ''
				};
				request.request({
					url: '/h5/order.confirm/detail',
					showLoading: true,
					token: true,
					data: params,
					success(res) {
						if (res.code == 200) {
							let order = res.data.detail;
							this.orderParams = order.order;
							if (checkObjLen(order.baby)) this.babyParams = order.baby;
							this.incomeParams = order.income;
							console.log(this.orderParams);
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							});
						}
					}
				});
			},
			// 时间选择
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 2;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},

			//获取缓存信息
			getTorge() {
				let setOrderData = uni.getStorageSync('setOrder');
				console.log('从缓存获取', setOrderData);
				if (setOrderData) {
					this.tagTitleList = setOrderData.tagTitleList;
					Object.assign(this.$data, setOrderData);
				}
				this.$forceUpdate();
			},

			// 提交订单
			submit() {
				let that = this;
				let getEventDate = new Date(that.orderParams.event_date); //获取举办
				let getSignDate = new Date(that.orderParams.sign_date); //获取签单时间
				let getTailTime = new Date(that.orderParams.tail_money_date); //获取尾款时间
				let getMiddleTime = new Date(that.orderParams.middle_money_date); //获取中款时间
				let getTail = getTailTime.getTime();
				let getEvent = getEventDate.getTime();
				let getMiddle = getMiddleTime.getTime();
				let getSign = getSignDate.getTime();
				let weikuan = getEventDate.setMonth(getEventDate.getMonth() + 3);

				if (that.incomeParams.income_type != 7 && that.orderParams.order_nature == 1 && getTailTime > 0) {
					if (that.orderParams.news_type == '8') {
						//会务尾款时间高于举办时间3个月
						if (getTail < getSign) {
							uni.showToast({
								icon: 'none',
								title: '尾款时间低于订单时间~'
							});
							return false;
						} else if (getTail > weikuan) {
							uni.showToast({
								icon: 'none',
								title: '尾款时间高于举办时间3个月~'
							});
							return false;
						}
					} else {
						if (getTail < getSign) {
							uni.showToast({
								icon: 'none',
								title: '尾款时间低于订单时间~'
							});
							return false;
						} else if (getTail > getEvent) {
							uni.showToast({
								icon: 'none',
								title: '尾款时间高于举办时间~'
							});
							return false;
						}
					}
				}

				if (that.incomeParams.income_type != 7 && that.orderParams.order_nature == 1 && that.orderParams
					.news_type != 7 && getMiddleTime > 0) {
					if (getMiddle < getSign) {
						//console.log(getMiddle + getSign + getMiddle + getEvent)
						uni.showToast({
							icon: 'none',
							title: '中款时间低于订单时间~'
						});
						return false;
					} else if (getMiddle > getEvent) {
						uni.showToast({
							icon: 'none',
							title: '中款时间高于举办时间~'
						});
						return false;
					}
				}
				let params = Object.assign({
						order: that.orderParams
					}, {
						member_id: that.member_id
					}, {
						member_allocate_id: that.member_allocate_id
					}, {
						banquet: that.banquetParams
					}, {
						wedding: that.weddingParams
					}, {
						baby: that.babyParams
					}, {
						meeting: that.meetingParams
					}, {
						activities: that.activitiesParams
					}, {
						car: that.carParams
					}, {
						settlement: that.settleParams
					}, {
						addons: that.addons
					}, {
						income: that.incomeParams
					}

				);
				that.disabled = true;
				request.request({
					url: '/api/audit.order/create',
					showLoading: true,
					token: true,
					data: params,
					success(res) {
						if (res.code == 200) {
							if (uni.getStorageSync('setOrder')) {
								uni.removeStorageSync('setOrder');
							}

							uni.showToast({
								icon: 'none',
								title: res.msg
							});

							uni.reLaunch({
								url: '/pages/order/index'
							});
						} else {
							that.disabled = false
							uni.showToast({
								icon: 'none',
								title: res.msg
							});
						}
					}
				});
			}
		}
	};
</script>

<style lang="less">
	uni-page-body {
		padding-top: 0;
	}

	@import url('../components/common.less');
	@import url('./common/common.less');

	.order-container {
		padding-bottom: 102upx;

		.footer-box {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			padding: 0 16px;
			display: flex;
			align-items: center;
			height: 100upx;
			font-size: 28upx;
			background-color: #ffffff;
			border-top: 1px solid #dedede;
			z-index: 9;

			.footer-title {
				flex: 0 15%;
			}

			.footer-action-btn {
				flex: 1;
				text-align: end;

				button {
					background-color: #007aff;
					color: #ffffff;
				}

				button[disabled] {
					background-color: grey !important;
					opacity: 1
				}

				button+button {
					margin-left: 5px;
				}
			}
		}
	}
</style>
